package Netty.buffer;

import java.nio.ByteBuffer;

/**
 * @author caihe
 * @date 2021/11/15 10:45
 */
public class ByteBufferDemo_Compact {

    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(6);
        byteBuffer.put((byte) 1);
        byteBuffer.put((byte) 2);
        byteBuffer.put((byte) 3);
        byteBuffer.put((byte) 4);
        byteBuffer.put((byte) 5);
        
        
        byteBuffer.flip();
        byteBuffer.get();
        byteBuffer.get();
        // compact将已读的数据移除 将未读的数据左移到已读的位置上
        System.out.println(byteBuffer.compact());

        byteBuffer.put((byte) 7);
        byteBuffer.flip();

        System.out.println(byteBuffer.get());
        System.out.println(byteBuffer.get());
        System.out.println(byteBuffer.get());
        System.out.println(byteBuffer.get());
        System.out.println(byteBuffer.get());

        System.out.println();
        
        
        

    }
}
